package com.ruoyi.system.mysql.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.api.domain.SysOperLog;
import com.ruoyi.system.mysql.service.TCCService;
import io.seata.rm.tcc.api.BusinessActionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class TCCServiceInpl  implements TCCService {
    @Override
    public boolean prepare(SysOperLog operLog) {
        log.info("TCCServiceInpl prepare {}",operLog);
        if (operLog.getOperId() == 1) {
            log.error("TCCServiceInpl prepare error");
            return false;
        }
        return true;
    }

    @Override
    public boolean commit(BusinessActionContext context) {
        log.info("TCCServiceInpl commit");

        // 从上下文中获取一阶段传递的参数
        JSONObject actionContext = (JSONObject) context.getActionContext("operLog");
        actionContext.toJavaObject(SysOperLog.class);
        SysOperLog operLog = actionContext.toJavaObject(SysOperLog.class);;// context.getActionContext("operLog");

        if (operLog != null && operLog.getOperId() == 13) {
            log.error("TCCServiceInpl commit error");
            return true;
        }
        return true;
    }

    @Override
    public boolean rollback(BusinessActionContext context) {
        log.info("TCCServiceInpl rollback");
        return true;
    }
}
